package com.hjm.project.AgentApplyTailmoney.AgentApplyTailmoney.service.impl;

import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;

import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.fastdfs.FastDFSClient;
import com.hjm.project.clientImg.clientImg.domain.ClientImg;
import com.hjm.project.clientImg.clientImg.mapper.ClientImgMapper;
import com.hjm.project.terminal.pos.mapper.AgentApplyPosMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjm.project.AgentApplyTailmoney.AgentApplyTailmoney.mapper.AgentApplyTailmoneyMapper;
import com.hjm.project.AgentApplyTailmoney.AgentApplyTailmoney.domain.AgentApplyTailmoney;
import com.hjm.project.AgentApplyTailmoney.AgentApplyTailmoney.service.IAgentApplyTailmoneyService;
import com.hjm.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * 服务商尾款充值抵扣Service业务层处理
 * 
 * @author AgentApplyTailmoney
 * @date 2020-11-27
 */
@Service
public class AgentApplyTailmoneyServiceImpl implements IAgentApplyTailmoneyService 
{
    @Autowired
    private AgentApplyTailmoneyMapper agentApplyTailmoneyMapper;
    @Autowired
    private AgentApplyPosMapper agentApplyPosMapper;
    @Autowired
    private ClientImgMapper clientImgMapper;


    /**
     * 查询服务商尾款充值抵扣
     * 
     * @param id 服务商尾款充值抵扣ID
     * @return 服务商尾款充值抵扣
     */
    @Override
    public AgentApplyTailmoney selectAgentApplyTailmoneyById(Long id)
    {
        return agentApplyTailmoneyMapper.selectAgentApplyTailmoneyById(id);
    }

    /**
     * 查询服务商尾款充值抵扣列表
     * 
     * @param agentApplyTailmoney 服务商尾款充值抵扣
     * @return 服务商尾款充值抵扣
     */
    @Override
    public List<AgentApplyTailmoney> selectAgentApplyTailmoneyList(AgentApplyTailmoney agentApplyTailmoney)
    {
        return agentApplyTailmoneyMapper.selectAgentApplyTailmoneyList(agentApplyTailmoney);
    }
    @Override
    public List<AgentApplyTailmoney> selectAgentApplyTailmoneyLists(AgentApplyTailmoney agentApplyTailmoney)
    {
        return agentApplyTailmoneyMapper.selectAgentApplyTailmoneyLists(agentApplyTailmoney);
    }

    /**
     * 新增服务商尾款充值抵扣
     * 
     * @param agentApplyTailmoney 服务商尾款充值抵扣
     * @return 结果
     */
    @Override
    public int insertAgentApplyTailmoney(AgentApplyTailmoney agentApplyTailmoney)
    {
        return agentApplyTailmoneyMapper.insertAgentApplyTailmoney(agentApplyTailmoney);
    }

    /**
     * 修改服务商尾款充值抵扣
     * 
     * @param agentApplyTailmoney 服务商尾款充值抵扣
     * @return 结果
     */
    @Override
    public int updateAgentApplyTailmoney(AgentApplyTailmoney agentApplyTailmoney)
    {
        return agentApplyTailmoneyMapper.updateAgentApplyTailmoney(agentApplyTailmoney);
    }

    /**
     * 删除服务商尾款充值抵扣对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAgentApplyTailmoneyByIds(String ids)
    {
        return agentApplyTailmoneyMapper.deleteAgentApplyTailmoneyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除服务商尾款充值抵扣信息
     * 
     * @param id 服务商尾款充值抵扣ID
     * @return 结果
     */
    @Override
    public int deleteAgentApplyTailmoneyById(Long id)
    {
        return agentApplyTailmoneyMapper.deleteAgentApplyTailmoneyById(id);
    }

    @Override
    public List<Map> findAgentPage(Map param) {
        List<Map> agentPage = agentApplyTailmoneyMapper.findAgentPage(param);
        for (Map map : agentPage) {
            if (map.get("MOENY") == null) {
                map.put("MOENY","0");
            }
        }
        return agentApplyTailmoneyMapper.findAgentPage(param);
    }


    @Transactional
    @Override
    public int saveApplyTMoney(AgentApplyTailmoney applyTailmoney, MultipartFile[] imgFile) throws Exception{
        int result=0;
        if (imgFile == null || imgFile.length == 0) {
            throw  new BusinessException("支付凭证上传失败,请刷新重试");
        }
        try {
              MultipartFile multipartFile = imgFile[0];
                //判断格式是不是图片格式jpg png gif
                String fileName =  multipartFile.getOriginalFilename();
                if (fileName.endsWith(".jpg")||fileName.endsWith(".png")||fileName.endsWith(".gif")) {
                    //上传图片
                    Map<String, String> metaList = Maps.newHashMap();
                    metaList.put("fileName",fileName);
                    metaList.put("fileType", fileName.substring(fileName.lastIndexOf(".")+1));
                    //上传图片服务器
                    String fid = FastDFSClient.uploadFile(multipartFile, multipartFile.getOriginalFilename(), metaList);
                    String fastGroup = fid.substring(0, 17);
                    Map<String, Object> saveMap = Maps.newHashMap();
                    applyTailmoney.setFileid(fid);

                    //添加图片
                    ClientImg clientImg = new ClientImg();
                    clientImg.setClientno(applyTailmoney.getAgentNum());
                    clientImg.setFileid(fid);
                    clientImg.setFastdfsGroup(fastGroup);
                    clientImg.setCreatename(applyTailmoney.getAddMan());
                    clientImg.setObjType(Long.parseLong("1"));
                    clientImg.setType(Long.parseLong("44"));
                    clientImgMapper.insertclientImg(clientImg);
                } else {
                    throw  new BusinessException("不支持当前格式");
                }
               result=agentApplyTailmoneyMapper.insertAgentApplyTailmoney(applyTailmoney);
        } catch (Exception e) {
            throw e;
        }
        return result;
    }
}
